﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
   public  class custListBLL
    {
       private custListTableAdapter _custListAdapter = null;
       private custListTableAdapter custListAdapter
   {
       get 
       {
        if (_custListAdapter ==null) _custListAdapter =new custListTableAdapter ();
           return _custListAdapter ;

        }
   }
        /// <summary>
       /// 查询客户信息表
        /// </summary>
        /// <returns></returns>
        
       public orderDataSet .custListDataTable getcustListAll()
   {
      try
           {
               return custListAdapter.GetDatacustListAll();
           }
           catch(Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
   }
       /// <summary>
       /// 根据客户名称查询客户信息表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public orderDataSet.custListDataTable getcustListAll(string custName)
       {
           try
           {
               return custListAdapter.GetDatacustListBycustName (custName);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }

           /// <summary>
           /// 插入客户信息表
           /// </summary>
           /// <param name="row"></param>
           /// <returns></returns>
          
       }
       public int custListInsert(custListModel row)
       {
           int i = 0;
         SqlConnection conn = dataConn.connOpen();
         SqlTransaction tn = null;
         
           try
           {
               tn = conn.BeginTransaction();
               custListDAL custList = new custListDAL(tn, conn);
               custList.custListInsert(row);
               tn.Commit();
               return i;
           }
           catch (SqlException ex)
           {
               tn.Rollback();
               if (ex.Number == 2627)
               {
                   throw new Exception("这个客户编号已经存在");
               }
              

               throw new Exception(ex.Number.ToString() + ex.Message.ToString());

           }

           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());
              
           }
           finally
           {
               conn.Close();
           }
       }
        /// <summary>
        /// 更新客户信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int custListUpdate(custListModel row)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction();
               custListDAL custList = new custListDAL(tn, conn);
               custList.custListUpdate(row);
               tn.Commit();
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());
               
           }
           finally
           {
               conn.Close();
           }
       }
        /// <summary>
        /// 删除一行客户信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int custListDel(string custNum)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction();
               custListDAL custList = new custListDAL(tn, conn);
             custList .custListDel(custNum);
               tn.Commit();
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());
               
           }
           finally
           {
               conn.Close();
           }
       }
    }

}
